package com.baizhi.controller;


import com.baizhi.entity.CmfzAdmin;
import com.baizhi.entity.CmfzBanner;
import com.baizhi.service.AdminService;
import com.baizhi.service.BannerService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("banner")
public class BannerController {

    @Autowired
    private BannerService bannerService;

    @RequestMapping("selectAll")
    @ResponseBody
    public Map selectAll(Integer page, Integer limit){
        Page<CmfzBanner> cmfzBannerPage = bannerService.selectAll(page, limit);
        Map map = new HashMap<>();
        map.put("code",0);
        map.put("msg",0);
        map.put("count",cmfzBannerPage.getTotal());
        map.put("data",cmfzBannerPage.getRecords());
        return map;
    }
    @RequestMapping("upload")
    @ResponseBody
    public Map upload(MultipartFile file) throws Exception{
        String  newName= UUID.randomUUID().toString()+file.getOriginalFilename();
        file.transferTo(new File("E:\\upload",newName));
        Map map = new HashMap();
        map.put("newName",newName);
        return map;
    }
    @RequestMapping("insert")
    @ResponseBody
    public  Map   insert(CmfzBanner cmfzBanner){
        Map map = new HashMap<>();
        try {
            bannerService.insert(cmfzBanner);
            map.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag",false);
        }
        return map;
    }

   @RequestMapping("delete")
    @ResponseBody
    public  Map   delete(Integer id){
        Map map = new HashMap<>();
        try {
            bannerService.delete(id);
            map.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag",false);
        }
        return map;
    }

    @RequestMapping("selectOne")
    @ResponseBody
    public  CmfzBanner   selectOne(Integer id){
        return bannerService.showOne(id);
    }


    @RequestMapping("update")
    @ResponseBody
    public  Map  update(CmfzBanner cmfzBanner){
        Map map = new HashMap<>();
        try {
            bannerService.update(cmfzBanner);
            map.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag",false);
        }
        return map;
    }

    @RequestMapping("deleteAll")
    @ResponseBody
    public  Map  deleteAll(List<CmfzBanner> ids){
        Map map = new HashMap<>();
        try {
            bannerService.deleteAll(ids);
            map.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag",false);
        }
        return map;
    }



}
